package leetcode.每日一题;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2021/12/2 11:06
 * @description：https://leetcode-cn.com/problems/zigzag-conversion/
 */
public class Z字形变换 {
    @Test
    public void test() {
        Solution solution = new Solution();
        System.out.println(solution.convert("A", 1));
    }

    class Solution {
        public String convert(String s, int numRows) {
            if (numRows == 1) return s;
            int interval = numRows * 2 - 2;
            int index = interval;
            int n = s.length();
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < numRows; i++) {
                int cur = i;
                int factor = 0;
                // 到达最后一行
                if (index == 0) index = interval;
                while (cur < n) {
                    builder.append(s.charAt(cur));
                    if (factor % 2 == 0) {
                        cur += index;
                    } else {
                        cur += interval == index ? index : interval - index;
                    }
                    factor++;
                }
                index -= 2;
            }
            return builder.toString();
        }
    }
}
